package net.roseparadise.service.impl;

import java.math.BigInteger;
import java.util.List;

import net.roseparadise.dao.AlbumDAO;
import net.roseparadise.dao.MemberDAO;
import net.roseparadise.model.Album;
import net.roseparadise.service.PhotoService;
import net.roseparadise.vo.PhotoPortletVO;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class PhotoServiceImpl implements PhotoService {
	@Autowired
	private AlbumDAO albumDAO;
	@Autowired
	private MemberDAO memberDAO;
	
	public PhotoPortletVO getPortletContent(Integer albumid){
		PhotoPortletVO result = new PhotoPortletVO();
		return result;
	}
	public List<Album> getAlbumListManage(int memberid, String isstudio, int startRow, int maxCount) {
		List result = null;
		if("Y".equals(isstudio)){
			result = albumDAO.getStudioAlbumList(startRow, maxCount);
		}else{
			result = albumDAO.getMemberAlbumList(memberid,startRow, maxCount);
		}
		return (List<Album>)result;
	}
    public int countAlbumListManage(int memberid, String isstudio){
		BigInteger result =null;
		if("Y".equals(isstudio)){
			result = albumDAO.countStudioAlbumList();
		}else{
			result = albumDAO.countMemberAlbumList(memberid);
		}
		return result.intValue();
	}
    
    public void deleteAlbum(Integer id){
    	Album album = albumDAO.findById(id);	
		albumDAO.delete(album);
	}
    
	public void addAlbum(Album album){
		albumDAO.save(album);
	}
	
	public void updateAlbum(Integer id,String field,String value){
		Album album = albumDAO.findById(id);
		if("albumorder".equals(field)){
			album.setAlbumorder(Integer.parseInt(value));
		}else if("albumname".equals(field)){
			album.setAlbumname(value);
		}else if("isstudio".equals(field)){
			album.setIsstudio(value);
		}
    	System.out.println(new JSONObject(album).toString());		
		albumDAO.merge(album);
	}
}
